<?php

namespace app\admin\model;


use app\admin\validate\CheckArticle;
use app\common\impl\Comm;
use app\common\model\AdminBaseModel;
use Org\Tool;
use think\Exception;
use think\facade\Route;

class Advance extends AdminBaseModel implements Comm
{
    //表名
    protected $name = "advance";

    public function getAdminPageData($param = [], $order = 'id asc')
    {
        // TODO: Implement getAdminPageData() method.

        $where = [];
        if (isset($param['title']) && $param['title'] != '') {
            $where[] = ['title', 'like', "%" . $param['title'] . "%"];
        }

        if (!empty($param['is_show']) && (int)$param['is_show']!=0) {
            $status = (int)$param['is_show'] - 1;
            $where[] = ['is_show', '=', $status];
        }

        if (!empty($param['is_top']) && (int)$param['is_top']!=0) {
            $status = (int)$param['is_top'] - 1;
            $where[] = ['is_top', '=', $status];
        }

        if (!empty($param['is_recommend']) && (int)$param['is_recommend']!=0) {
            $status = (int)$param['is_recommend'] - 1;
            $where[] = ['is_recommend', '=', $status];
        }

        if (!empty($param['cate_id']) && (int)$param['cate_id']!=0) {
            $child=getChildsRule(ArticleCates::select()->toArray(),$param['cate_id'],false);
            $child[]=$param['cate_id'];
            $where[] = ['cate_id', 'in', $child];
        }

        if (isset($param['time']) && $param['time'] != '') {
            $ck = @explode(" ~ ", $param['time']);
            $b = $ck[0] . " 00:00:00";
            $e = $ck[1] . " 23:59:59";
            $where[] = ['create_time', 'between', [strtotime($b), strtotime($e)]];
        }

        try {
            $data = self::with("cates")->limit_select($where, $order);
            $count = self::hhy_count($where);

            return parent::ajaxResult($data, $count);
        } catch (Exception $exception) {
            return parent::ajaxResult([], 0, 100, $exception->getMessage());
        }
    }

    /**
    * @param $param
    * @return \think\response\Json
    * @author: yu
    * @describe:编辑或添加
    */
    public function doAll($param)
    {
        // TODO: Implement doAll() method.
        try {
            unset($param['file']);
            $check = new CheckArticle();
            if (!$check->check($param)) {
                return parent::JsonReturn($check->getError(), 0);
            }

            $param['content'] = htmlspecialchars_decode($param['content']);

            if (empty($param['description'])) {
                $param['description'] = Tool::str_cut($param['content'], 100);
            }

            if (isset($param['id'])) {
                $id=$param['id'];
                $res = self::update($param);
            }else{
                $res = self::save($param);
                $id=$this->id;
            }
            $u = Route::buildUrl("index/article/show", ['id' => $id])->__toString();
            if ($param['origin'] == '原创') {
                $this->edit(['id' => $id, 'url' => $u]);
            }
            if ($res) {
                return self::JsonReturn("更新添加成功");
            } else {
                return self::JsonReturn("更新添加失败", 0);
            }
        } catch (\Exception $e) {
            return self::JsonReturn($e->getMessage(), 0);
        }
    }


    /**
     * @param $id
     * @return \think\response\Json
     * @author: yu
     * @describe:软删除
     */
    public function delById($id)
    {
        try {
            // TODO: Implement delById() method.
            if (is_array($id)) {
                $ids = $id;
            } else {
                $ids = @explode(",", $id);
            }
            $result = self::destroy($ids);
            if ($result) {
                return self::JsonReturn("删除成功");
            } else {
                return self::JsonReturn("删除失败", 0);
            }
        } catch (\Exception $exception) {
            return self::JsonReturn($exception->getMessage(), 0);
        }
    }


}